import {removeToken, setToken} from '@/utils/auth'
import {getStore, setStore} from '@/utils/store'
import {getMenu, getTopMenu, getUserInfo, loginByUsername, logout, refeshToken} from '@/api/user'
import {formatPath} from "@/router/avue-router";

const user = {
  state: {
    userInfo: {},
    permission: {},
    roles: [],
    menuId: {},
    menu: getStore({name: 'menu'}) || [],
    menuAll: getStore({name: 'menuAll'}) || [],
    token: getStore({name: 'token'}) || '',
    SysId: getStore({name: 'SysId'}) || ''
  },
  actions: {
    //根据用户名登录
    LoginByUsername({commit}, userInfo = {}) {
      const user = userInfo
      return new Promise((resolve) => {
        loginByUsername(user.username, user.password, userInfo.code, userInfo.redomStr).then(res => {
          const data = res.data[1]
          commit('SET_TOKEN', data);
          commit('DEL_ALL_TAG', []);
          commit('CLEAR_LOCK');
          resolve();
        })
      })
    },
    //根据手机号登录
    LoginByPhone({commit}, userInfo) {
      return new Promise((resolve) => {
        loginByUsername(userInfo.phone, userInfo.code).then(res => {
          const data = res.data.data;
          commit('SET_TOKEN', data);
          commit('DEL_ALL_TAG', []);
          commit('CLEAR_LOCK');
          resolve();
        })
      })
    },
    GetUserInfo({commit}, token) {
      return new Promise((resolve, reject) => {
        getUserInfo(token).then((res) => {
          const data = res.data[1]
          commit('SET_USERIFNO', data);
          commit('SET_TOKEN', token);
          commit('SET_ROLES', token);
          // commit('SET_PERMISSION', data)
          resolve(data);
        }).catch(err => {
          reject(err);
        })
      })
    },
    //刷新token
    RefeshToken({state, commit}) {
      return new Promise((resolve, reject) => {
        refeshToken(state.refeshToken).then(res => {
          const data = res.data.data;
          commit('SET_TOKEN', data);
          resolve(data);
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    LogOut({commit}) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_TOKEN', '')
          commit('SET_MENUALL_NULL', []);
          commit('SET_MENU', [])
          commit('SET_ROLES', [])
          commit('DEL_ALL_TAG', []);
          commit('SET_CURRENT_PLATSYSID', '');
          commit('CLEAR_LOCK');
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    //注销session
    FedLogOut({commit}) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_MENUALL_NULL', []);
        commit('SET_MENU', [])
        commit('SET_ROLES', [])
        commit('DEL_ALL_TAG', []);
        commit('SET_CURRENT_PLATSYSID', '');
        commit('CLEAR_LOCK');
        removeToken()
        resolve()
      })
    },
    GetTopMenu() {
      return new Promise(resolve => {
        getTopMenu().then((res) => {
          const data = res.data[1] || []
          resolve(data.menus)
        })
      })
    },
    //获取系统菜单
    GetMenu({commit}, parentId) {
      return new Promise(resolve => {
        getMenu(parentId.PLATSYSID).then((res) => {
          const data = res.data[1]
          data.menus.forEach(ele => formatPath(ele, true));
          commit('SET_MENU', data.menus)
          commit('SET_MENUALL', data.menus)
          commit('SET_CURRENT_PLATSYSID', parentId.PLATSYSID)
          resolve(data.menus)
        })
      })
    },
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      setToken(token)
      state.token = token;
      setStore({name: 'token', content: state.token})
    },
    SET_MENUID(state, menuId) {
      state.menuId = menuId;
    },
    SET_USERIFNO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    SET_MENUALL: (state, menuAll) => {
      let menu = state.menuAll;
      menuAll.forEach(ele => {
        let index = menu.findIndex(item => item.path == ele.path)
        if (index == -1) {
          menu.push(ele);
        } else {
          menu[index] = ele;
        }
      })
      state.menuAll = menu
      setStore({name: 'menuAll', content: state.menuAll})
    },
    SET_MENUALL_NULL: (state) => {
      state.menuAll = []
      setStore({name: 'menuAll', content: state.menuAll})
    },
    SET_MENU: (state, menu) => {
      state.menu = menu
      setStore({name: 'menu', content: state.menu})
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    SET_PERMISSION: (state, permission) => {
      state.permission = {};
      permission.forEach(ele => {
        state.permission[ele] = true;
      });
    },
    SET_CURRENT_PLATSYSID: (state, SYSID) => {
      state.SysId = SYSID
      setStore({name: 'SysId', content: SYSID})
    }
  }

}
export default user
